//
//  VC.swift
//  bibao
//
//  Created by Zhongnan on 16/3/2.
//  Copyright © 2016年 Zhongnan. All rights reserved.
//

import UIKit


class LDGuiderImgVC: UIViewController,UIScrollViewDelegate {
    
    var scrollView:UIScrollView!
    var pageControl:UIPageControl!
    var startBtn:UIButton!
    
    //图片数量
    var guidePicArr = [String]() {
        
        didSet{
            
            loadCustomLayout()
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.sharedApplication().statusBarHidden = true
        view.backgroundColor = LDGolbalColor.BgGoundGray
    }
    
    //scrollview委托
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let offset=scrollView.contentOffset
        
        pageControl.currentPage=Int(offset.x/LDFrame.ScreenWidth)
        if(pageControl.currentPage == guidePicArr.count-1){
            
            startBtn.hidden=false
        }else{
            
            startBtn.hidden=true
        }
    }
    //载入自定义布局
    func loadCustomLayout(){
        
        scrollView=UIScrollView(frame: CGRectMake(0, 0, LDFrame.ScreenWidth, LDFrame.ScreenHeight))
        scrollView.pagingEnabled=true
        scrollView.showsHorizontalScrollIndicator=false
        scrollView.showsVerticalScrollIndicator=false
        scrollView.contentSize=CGSizeMake(CGFloat(guidePicArr.count) * LDFrame.ScreenWidth, LDFrame.ScreenHeight)
    
        for i in 1...guidePicArr.count{
            
            let strname = guidePicArr[i-1]
            let image=UIImage(named: strname)
            let imageView=UIImageView(image: image)
            
            imageView.frame=CGRectMake(CGFloat(i-1)*LDFrame.ScreenWidth, 0, LDFrame.ScreenWidth, LDFrame.ScreenHeight)
            imageView.contentMode = UIViewContentMode.ScaleAspectFit
            scrollView.addSubview(imageView)
            
        }
        
        pageControl=UIPageControl(frame: CGRectMake(LDFrame.ScreenWidth/2-100, LDFrame.ScreenHeight-60, 200, 45))
        pageControl.currentPageIndicatorTintColor = LDIconColor.Black
        pageControl.pageIndicatorTintColor = LDIconColor.DeepGray
        
        startBtn=UIButton(frame: CGRectMake(LDFrame.ScreenWidth/2-60, LDFrame.ScreenHeight-100, 100, 30))
        startBtn.center.x = self.view.center.x
        
        pageControl.numberOfPages=guidePicArr.count;
        pageControl.currentPage=0
        
        startBtn.setBackgroundImage(UIImage(named: "welcome_enter"), forState: UIControlState.Normal)

        startBtn.addTarget(self, action: #selector(self.guideOver), forControlEvents: UIControlEvents.TouchUpInside)
        startBtn.hidden=true
        
        
        scrollView.bounces=false
        scrollView.delegate=self
        self.view.addSubview(scrollView)
        self.view.addSubview(pageControl)
        self.view.addSubview(startBtn)
        
    }
    
    //开始使用app
    func guideOver(){
        //点击按钮发送加载完成通知
        NSNotificationCenter.defaultCenter().postNotificationName(NOTI_GUIDERIMGVIEW_END, object: nil)
        UIApplication.sharedApplication().statusBarHidden = false
    }
    
    
}
